package demo3;

public class Solution {

    public int massage(int[] nums) {
        int n = nums.length;

        int[] f = new int[n + 1];
        int[] g = new int[n + 1];

        for(int i = 1; i <= n; i++){
            f[i] = g[i - 1] + nums[i - 1];
            g[i] = Math.max(g[i - 1], f[i - 1]);
        }
        return Math.max(f[n], g[n]);
    }


    public int rob(int[] nums) {
        int n = nums.length;

        return Math.max(nums[0] + get(nums, 2, n - 2), get(nums, 1, n - 1));
    }
    public int get(int[] nums, int left, int right){
        if(left > right) { return 0;}
        int[] f = new int[nums.length];
        int[] g = new int[nums.length];
        f[left] = nums[left];

        for(int i = left + 1; i <= right; i++){
            f[i] = g[i - 1] + nums[i];
            g[i] = Math.max(g[i - 1], f[i - 1]);
        }
        return Math.max(f[right], g[right]);
    }

    public int deleteAndEarn(int[] nums) {
        int n = 10001;
        int[] arr = new int[n];
        for(int x : nums){
            arr[x] += x;
        }

        int[] f = new int[n];
        int[] g = new int[n];
        f[0] = arr[0];

        for(int i = 1; i < n; i++){
            f[i] =  arr[i] + g[i - 1];
            g[i] = Math.max(g[i - 1], f[i - 1]);
        }
        return Math.max(f[n - 1], g[n - 1]);
    }
}
